<!-- 商品详细页 -->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddDialog">添加分类</el-button>
          <!-- 表格 -->
          <tree-table
            :data="cateList"
            :columns="columns"
            :expand-type="false"
            :selection-type="false"
            show-index
            border
          >
            <!-- 是否有效 -->
            <template #isok="scope">
              <i
                v-if="!scope.row.cat_deleted"
                style="color:rgb(51,153,159)"
                class="el-icon-success"
              ></i>
              <i v-else class="el-icon-error" style="color:red"></i>
            </template>
            <!-- 排序 -->
            <template #order="scope">
              <el-tag size="mini" v-if="scope.row.cat_level == 0">一级</el-tag>
              <el-tag
                size="mini"
                type="success"
                v-else-if="scope.row.cat_level == 1"
                >二级</el-tag
              >
              <el-tag size="mini" type="warning" v-else>三级</el-tag>
            </template>
            <!-- 操作 -->
            <template #opt>
              <el-button size="mini" type="primary" icon="el-icon-edit"
                >编辑</el-button
              >
              <el-button size="mini" type="danger" icon="el-icon-delete"
                >删除</el-button
              >
            </template>
          </tree-table>
          <!-- 分页栏 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="querInfo.pagenum"
            :page-sizes="[1, 2, 3, 5]"
            :page-size="querInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog
      @close="addCateDialogClosed"
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="50%"
    >
      <!-- 添加分类的表单 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            :options="parentCateList"
            :props="cascaderProps"
            expand-trigger="hover"
            v-model="selectedList"
            @change="parentCateChanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data() {
    return {
      cateList: [],
      // 初始化获取的信息
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 3
      },
      total: 0,
      // 树形表格里的栏目
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_id: 0,
        cat_level: 1
      },
      addCateFormRules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      },
      // 父级分类列表
      parentCateList: [],
      // 多层级菜单的数据
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      // 选中父级节点的id
      selectedList: []
    }
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.querInfo
      })
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)

      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 每页显示多少条数据
    handleSizeChange(newSize) {
      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    // 当前页面的变化
    handleCurrentChange(newPage) {
      this.querInfo.pagenum = newPage
      this.getCateList()
    },
    showAddDialog() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    // 获取一,二级的列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status != 200) {
        return this.$message.error('获取父级分类数据失败')
      }
      this.parentCateList = res.data
    },
    parentCateChanged() {
      console.log(this.selectedList)
      if (this.selectedList.length > 0) {
        // 获取选择的父级节点id
        this.addCateForm.cat_id = this.selectedList[
          this.selectedList.length - 1
        ]
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = this.selectedList.length
      } else {
        this.addCateForm.cat_id = 0
        this.addCateForm.cat_level = 0
      }
    },
    addCate() {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请输入分类名称')
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        if (res.meta.status != 201) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        console.log(this.cateList)
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    addCateDialogClosed() {
      // 对表单里的信息进行重置
      this.$refs.addCateFormRef.resetFields()
      this.selectedList = []
      this.addCateForm.cat_id = 0
      this.addCateForm.cat_level = 0
    }
  },
  created() {
    this.getCateList()
  }
}
</script>
<style lang="less" scoped>
.el-button {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
